#include <QtGui>
#include "identifydialog.h"
#include "imageprocesser.h"


IdentifyDialog::IdentifyDialog(QImage *img, QWidget *parent) : QDialog(parent){

    QVBoxLayout *mainLayout = new QVBoxLayout;
    QHBoxLayout *topLayout = new QHBoxLayout;
    QVBoxLayout *parameterLayout = new QVBoxLayout;
    thresholdSlider = new QSlider(Qt::Horizontal);
    QLabel *originalImage = new QLabel;
    procImage = new QLabel;
    QHBoxLayout *sliderLayout = new QHBoxLayout;
    QPushButton *okButton = new QPushButton("Ok");
    QPushButton *cancelButton = new QPushButton("Cancel");
    QHBoxLayout *buttonsLayout = new QHBoxLayout;
    this->img = new QImage(*img);
    ImageProcesser *aux = new ImageProcesser(img);
    dilateSpin = new QSpinBox;
    erodeSpin = new QSpinBox;
    QGroupBox *thresholdGroup = new QGroupBox(tr("Threshold"));
    QGroupBox *erodeGroup = new QGroupBox(tr("Erode"));
    QGroupBox *dilateGroup = new QGroupBox(tr("Dilate"));
    QVBoxLayout *erodeLayout = new QVBoxLayout;
    QVBoxLayout *dilateLayout = new QVBoxLayout;
    QVBoxLayout *thresholdLayout = new QVBoxLayout;
    thresholdSpin = new QSpinBox;

    unsigned char autoThreshold;

    originalImage->setScaledContents(true);
    procImage->setScaledContents(true);

    originalImage->setPixmap(QPixmap::fromImage(*img));
    originalImage->setMaximumSize(QSize(500,500*originalImage->height()/originalImage->width()));
    aux->Ecualize();
    autoThreshold = aux->GetAutomaticThreshold();
    aux->Threshold(autoThreshold);
    procImage->setPixmap(QPixmap::fromImage(*(aux->GetImage())));
    procImage->setMaximumSize(QSize(500,500*procImage->height()/procImage->width()));

    thresholdSlider->setRange(0,255);
    thresholdSlider->setFixedSize(thresholdSlider->sizeHint());
    thresholdSlider->setValue(autoThreshold);
    thresholdSpin->setRange(0,255);
    thresholdSpin->setValue(autoThreshold);

    dilateSpin->setValue(1);
    erodeSpin->setValue(1);

    sliderLayout->addWidget(thresholdSpin);
    sliderLayout->addWidget(thresholdSlider);
    dilateLayout->addWidget(dilateSpin);
    erodeLayout->addWidget(erodeSpin);

    thresholdLayout->addLayout(sliderLayout);

    thresholdGroup->setLayout(thresholdLayout);
    erodeGroup->setLayout(erodeLayout);
    dilateGroup->setLayout(dilateLayout);

    parameterLayout->addWidget(thresholdGroup);
    parameterLayout->addWidget(erodeGroup);
    parameterLayout->addWidget(dilateGroup);
    parameterLayout->setAlignment(Qt::AlignTop);

    buttonsLayout->addWidget(okButton);
    buttonsLayout->addWidget(cancelButton);
    buttonsLayout->setAlignment(Qt::AlignRight);

    topLayout->addLayout(parameterLayout);
    topLayout->addWidget(originalImage);
    topLayout->addWidget(procImage);

    mainLayout->addLayout(topLayout);
    mainLayout->addLayout(buttonsLayout);

    setLayout(mainLayout);

    connect(thresholdSlider,SIGNAL(sliderReleased()),this,SLOT(ThresholdSliderChanged()));
    connect(thresholdSpin,SIGNAL(valueChanged(int)),this,SLOT(ThresholdSpinChanged(int)));
    connect(erodeSpin,SIGNAL(valueChanged(int)),this,SLOT(TestParameters()));
    connect(dilateSpin,SIGNAL(valueChanged(int)),this,SLOT(TestParameters()));
    connect(okButton,SIGNAL(clicked()),this,SLOT(accept()));
    connect(cancelButton,SIGNAL(clicked()),this,SLOT(reject()));

    setWindowTitle("Parameters");



}

void IdentifyDialog::ThresholdSliderChanged(){
    thresholdSpin->setValue(thresholdSlider->value());
    TestParameters();

}

void IdentifyDialog::ThresholdSpinChanged(int t){
    thresholdSlider->setValue(t);
    TestParameters();
}


unsigned char IdentifyDialog::GetThreshold(){
    return thresholdSlider->value();
}

unsigned char IdentifyDialog::GetErodeSize(){
    return erodeSpin->value();
}
unsigned char IdentifyDialog::GetDilateSize(){
    return dilateSpin->value();
}

void IdentifyDialog::TestParameters(){
    ImageProcesser *aux = new ImageProcesser(img);
    aux->Ecualize();
    aux->Threshold(thresholdSlider->value());
    aux->Erode(erodeSpin->value());
    aux->Dilate(dilateSpin->value());
    procImage->setPixmap(QPixmap::fromImage(*(aux->GetImage())));
    procImage->setMaximumSize(QSize(500,500*procImage->height()/procImage->width()));
}
